package com.fate.controller;

import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.sun.org.apache.xpath.internal.operations.Mult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashSet;

@RestController
@Api(tags = "文件管理接口")
public class FileController {
    @Autowired
    private FastFileStorageClient fastFileStorageClient;

    @Value("${resources.url}")
    private String host;

    /**
     * 文件上传到fastdfs
     * fastdfs 端口里面有一个nginx 默认是80
     */
    @PostMapping("admin/file/upload/element")
    @ApiOperation("文件上传接口")
    public ResponseEntity<String> fileUpload(@RequestParam MultipartFile file) {
        if (ObjectUtils.isEmpty(file)) {
            return ResponseEntity.ok().body("文件为空");
        }
        try {
            //文件上传到文件服务器
            String fullPath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), "jpg", new HashSet<>()).getFullPath();
            String path = host + "/" + fullPath;
            return ResponseEntity.ok(path);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ResponseEntity.ok().build();
    }
}
